package com.apes.fn.scm.appScan;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.rbac.model.LocalPermitItem;
import com.apes.scm.rbac.repository.LocalPermitItemRepository;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 公共模板类
 */
public abstract class SimpleTemplate extends DomainService implements Template {

    @Autowired
    private LocalPermitItemRepository localPermitItemRepository;

    private static final JSONObject emptyJSON = new JSONObject();
    private static final JSONArray emptyJSONArray = new JSONArray();

    @Override
    public JSONObject getOrder(String id) {
        return emptyJSON;
    }

    @Override
    public JSONArray getOrderLists(SimpleRequest request, String origin) {
        return emptyJSONArray;
    }

    @Override
    public void begin(String id, String userId) {
    }

    @Override
    public void cancel(String id, String userId) {
    }

    /**
     * 根据登录信息获取可操作的部门
     *
     * @param request deptRepository
     * @return List<DeptId>
     */
    protected List<String> getOperatorDept(SimpleRequest request) {
        List<LocalPermitItem> localPermitItems = localPermitItemRepository.find(request.getPostId(), "pp_operate", "pt_dept");
        if (localPermitItems.isEmpty()) throw new RuntimeException("当前登录人员没有配置可操作权限，将检查本地权限配置");

        return localPermitItems.stream().map(LocalPermitItem::getOrgId).collect(Collectors.toList());
    }

    /**
     * 根据登录信息获取可操作的仓库
     *
     * @param request deptRepository
     * @return List<DeptId>
     */
    protected List<String> getOperatorLocation(SimpleRequest request) {
        List<LocalPermitItem> localPermitItems = localPermitItemRepository.find(request.getPostId(), "pp_operate", "pt_location");
        if (localPermitItems.isEmpty()) throw new RuntimeException("当前登录人员没有配置可操作权限，将检查本地权限配置");

        return localPermitItems.stream().map(LocalPermitItem::getOrgId).collect(Collectors.toList());
    }
}
